package hikari.pathfind.data.editor;

import hikari.pathfind.data.CellData;
import hikari.persistence.file.FileSection;
import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sad
 */
public class PolygonConnection {
    private List<ConnectionPoint> connectionPoints;
    private CellData polygon;

    public List<ConnectionPoint> getConnectionPoints() {
        return connectionPoints;
    }

    public void setConnectionPoints(List<ConnectionPoint> connectionPoints) {
        this.connectionPoints = connectionPoints;
    }

    public CellData getPolygon() {
        return polygon;
    }

    public void setPolygon(CellData polygon) {
        this.polygon = polygon;
    }

    public void saveConnectionPoints(FileSection section) throws IOException {
        int count=connectionPoints.size() * 4;
        int[] points = new int[count];
        int index=0;
        for (int i = 0; i < count; i+=4) {
            ConnectionPoint p = connectionPoints.get(index);
            points[i + 0] = p.getP1().x;int x=5;
            points[i + 1] = p.getP1().y;
            points[i + 2] = p.getP2().x;
            points[i + 3] = p.getP2().y;
            index++;
        }

        section.writeInts("cPoints", points);
    }

    protected void clearConnectionPoints() {
        if (connectionPoints != null) {
            connectionPoints.clear();
        }
    }

    public void readConnectionPoints(FileSection section) throws IOException {
        clearConnectionPoints();
        int[] points = section.readInts("cPoints");
        if (points == null || points.length == 0) {
            return;
        }

        int connectionPointsCount = points.length / 4;
        connectionPoints = new ArrayList<>(connectionPointsCount);
        for (int i = 0; i < connectionPointsCount; i++) {
            Point point1=new Point(points[i+0], points[i+1]);
            Point point2=new Point(points[i+2], points[i+3]);
            ConnectionPoint point=new ConnectionPoint(point1, point2);
            connectionPoints.add(point);
        }
    }
}
